import api from '@/api';

export default {
  namespace: 'warehouse',

  state: {
    warehouseInfo: {},
    warehouse: {
      list: [],
      pagination: {},
    },
    combos: [],
    users: {
      list: [],
      pagination: {},
    },
    groupUsers: {
      list: [],
      pagination: {},
    },
    incidentals: {
      list: [],
      pagination: {},
    },
    currentFreight: {},
    currentIncidental: {},
  },

  effects: {
    *checkName({ payload }, { call, put }) {
      return yield call(api.warehouse.checkName, payload);
    },
    *checkCode({ payload }, { call, put }) {
      return yield call(api.warehouse.checkCode, payload);
    },
    *createWarehouse({ payload }, { call, put }) {
      return yield call(api.warehouse.create, payload);
    },
    *updateWarehouse({ payload }, { call, put }) {
      return yield call(api.warehouse.update, payload);
    },
    *disableWarehouse({ payload }, { call, put }) {
      return yield call(api.warehouse.disable, payload);
    },
    *enableWarehouse({ payload }, { call, put }) {
      return yield call(api.warehouse.enable, payload);
    },
    *deleteWarehouse({ payload }, { call, put }) {
      return yield call(api.warehouse.delete, payload);
    },
    *fetchWarehouseInfo({ payload }, { call, put }) {
      const res = yield call(api.warehouse.queryInfo, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveWarehouseInfo',
          payload: res.data,
        });
      }
    },
    *fetchWarehouse({ payload }, { call, put }) {
      const res = yield call(api.warehouse.queryList, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveWarehouse',
          payload: {
            list: res.data,
            pagination: res.pagination,
          },
        });
      }
    },
    *fetchUsers({ payload }, { call, put }) {
      const response = yield call(api.role.queryUsers, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveUsers',
          payload: {
            list: response.data,
            pagination: response.pagination,
          },
        });
      }
    },
    *fetchUsersByGroupId({ payload }, { call, put }) {
      const response = yield call(api.warehouse.queryUsersByGroupId, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveGroupUsers',
          payload: {
            list: response.data,
            pagination: response.pagination,
          },
        });
      }
    },
    *deleteUsers({ payload }, { call, put }) {
      return yield call(api.warehouse.deleteUsers, payload);
    },
    *assignUsers({ payload }, { call, put }) {
      return yield call(api.warehouse.assignUsers, payload);
    },

    *fetchCurrentFreight({ payload }, { call, put }) {
      const res = yield call(api.price.queryFreightById, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveCurrentFreight',
          payload: res.data,
        });
      }
      return res;
    },
    *addFreight({ payload }, { call }) {
      return yield call(api.price.createFreight, payload);
    },
    *updateFreight({ payload }, { call }) {
      return yield call(api.price.updateFreight, payload);
    },
    *queryFreightDetail({ payload }, { call, put }) {
      return yield call(api.price.queryWeightAndRegionInfo, payload);
    },
    *addFreightSheets({ payload }, { call }) {
      return yield call(api.price.createFreightSheets, payload);
    },
    *fetchFreightSheets({ payload }, { call }) {
      return yield call(api.price.queryFreightSheets, payload);
    },
    *submitAuditFreight({ payload }, { call }) {
      return yield call(api.price.submitAuditFreight, payload);
    },
    *auditFreight({ payload }, { call }) {
      return yield call(api.price.auditFreight, payload);
    },
    *fetchIncidentals({ payload }, { call, put }) {
      const res = yield call(api.price.queryIncidentals, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveIncidentals',
          payload: {
            list: res.data,
            pagination: res.pagination,
          },
        });
      }
      return res;
    },
    *fetchCurrentIncidental({ payload }, { call, put }) {
      const res = yield call(api.price.queryIncidentalById, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveCurrentIncidental',
          payload: res.data,
        });
      }
      return res;
    },
    *addIncidental({ payload }, { call }) {
      return yield call(api.price.createIncidental, payload);
    },
    *updateIncidental({ payload }, { call }) {
      return yield call(api.price.updateIncidental, payload);
    },
    *deleteIncidentals({ payload }, { call, put }) {
      return yield call(api.price.removeIncidentals, payload);
    },
    *queryIncidentalDetail({ payload }, { call, put }) {
      return yield call(api.price.queryWeightAndRegionInfo, payload);
    },
    *addIncidentalSheets({ payload }, { call }) {
      return yield call(api.price.createIncidentalSheets, payload);
    },
    *fetchIncidentalSheets({ payload }, { call }) {
      return yield call(api.price.queryIncidentalSheets, payload);
    },
    *submitAuditIncidental({ payload }, { call }) {
      return yield call(api.price.submitAuditIncidental, payload);
    },
    *auditIncidental({ payload }, { call }) {
      return yield call(api.price.auditIncidental, payload);
    },
    *fetchDimensions(_, { call }) {
      return yield call(api.price.queryDimensions);
    },
  },

  reducers: {
    saveWarehouseInfo(state, { payload }) {
      return {
        ...state,
        warehouseInfo: payload,
      };
    },
    saveWarehouse(state, { payload }) {
      return {
        ...state,
        warehouse: payload,
      };
    },
    saveUsers(state, { payload }) {
      return {
        ...state,
        users: payload,
      };
    },
    saveGroupUsers(state, { payload }) {
      return {
        ...state,
        groupUsers: payload,
      };
    },

    saveCombos(state, { payload }) {
      return {
        ...state,
        combos: payload,
      };
    },
    saveCurrentFreight(state, { payload }) {
      return {
        ...state,
        currentFreight: payload,
      };
    },
    clearCurrentFreight(state) {
      return {
        ...state,
        currentFreight: {},
      };
    },
    saveIncidentals(state, { payload }) {
      return {
        ...state,
        incidentals: payload,
      };
    },
    saveCurrentIncidental(state, { payload }) {
      return {
        ...state,
        currentIncidental: payload,
      };
    },
    clearCurrentIncidental(state) {
      return {
        ...state,
        currentIncidental: {},
      };
    },
  },
};
